package com.outlandr.irc.server;

import java.util.ArrayList;
import java.util.List;

public class Channel {
	
	List<User> users = new ArrayList<User>();
	private String channelName;
	private String topic = "Channel topic";

	public Channel(String channelName) {
		this.channelName = channelName;
	}

	public synchronized void join(User user) {
		if (!users.contains(user)) {
			users.add(user);
		}
	}
	
	public synchronized void leave(User user) {
		if (users.contains(user)) {
			users.remove(user);
		}
	}
	
	public void sendMessage(Message message) {
		//:koolhead17!n=atul@unaffiliated/koolhead17 PRIVMSG ##security :+ckh:gud luck
		
		User sender = message.getSender();
		String msg = String.format(":%s!n=%s@%s PRIVMSG %s :%s", 
				sender.getNick(), sender.getUserName(), sender.getHostName(), getName(), message.getMessage());
		
		System.out.println("Sending: " + msg);

		
		for (User user : users) {
			// If the user is not the original sender, then send the message
			if (!user.getNick().equals(message.getSender().getNick())) {
				user.sendMessage(msg);
			}
		}
	}
	
	public void sendMessage(String msg) {
		for (User user : users) {
			user.sendMessage(msg);
		}
	}

	public User[] getUsers() {
		return (User[]) users.toArray(new User[users.size()]);
	}

	public String getName() {
		return channelName;
	}

	public String getMode() {
		// return channel mode
		// TODO figure out what modes are needed
		return "+tncP"; 
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	
	
	
}
